using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MKDS_Course_Editor
{
    class ssar
    {
        public static SSAR_File Read(byte[] file)
        {
            SSAR_File s = new SSAR_File();
            EndianBinaryReader er = new EndianBinaryReader(new MemoryStream(file), Endianness.LittleEndian);
            if ((s.Header.Type = er.ReadString(Encoding.ASCII, 4)) == "SSAR")
            {
                s.Header.Magic = er.ReadUInt32();
                s.Header.FileSize = er.ReadInt32();
                s.Header.BlockSize = er.ReadInt16();
                s.Header.NrBlocks = er.ReadInt16();
                //Data
                if ((s.Data.Type = er.ReadString(Encoding.ASCII, 4)) == "DATA")
                {
                    s.Data.BlockSize = er.ReadInt32();
                    s.Data.DataOffset = er.ReadInt32();
                    s.Data.NrRecords = er.ReadInt32();
                    s.Data.Records = new SSAR_File.data.SSAR_Record[s.Data.NrRecords];
                    for (int i = 0; i < s.Data.NrRecords; i++)
                    {
                        s.Data.Records[i] = new SSAR_File.data.SSAR_Record();
                        s.Data.Records[i].Offset = er.ReadInt32();
                        s.Data.Records[i].bnk = er.ReadInt16();
                        s.Data.Records[i].vol = er.ReadByte();
                        s.Data.Records[i].cpr = er.ReadByte();
                        s.Data.Records[i].ppr = er.ReadByte();
                        s.Data.Records[i].ply = er.ReadByte();
                        s.Data.Records[i].reserved = er.ReadUInt16();
                    }
                    er.BaseStream.Position = s.Data.DataOffset;
                    s.Data.NoteData = er.ReadBytes((int)(er.BaseStream.Length - er.BaseStream.Position));
                }
                else
                {
                    System.Windows.MessageBox.Show("Error");
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Error");
            }
            er.Close();
            return s;
        }

        public struct SSAR_File
        {
            public header Header;
            public struct header
            {
                public string Type;
                public UInt32 Magic;
                public Int32 FileSize;
                public Int16 BlockSize;
                public Int16 NrBlocks;
            }
            public data Data;
            public struct data
            {
                public string Type;
                public Int32 BlockSize;
                public Int32 DataOffset;
                public Int32 NrRecords;
                public SSAR_Record[] Records;
                public struct SSAR_Record
                {
                    public Int32 Offset;	// relative offset of the archived SEQ file, absolute offset = Offset + Header.DataOffset
                    public Int16 bnk;		// bank
                    public byte vol;		// volume
                    public byte cpr;		// channel pressure 
                    public byte ppr;		// polyphonic pressure
                    public byte ply;		// play
                    public UInt16 reserved;
                }
                public byte[] NoteData;
            }
        }
    }
}
